package com.meichuang.meipian.video.textbitmap;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import static android.opengl.GLES20.*;

import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.opengl.GLUtils;
import android.text.TextPaint;

import org.json.JSONObject;
/**
 * Created by ken on 17/11/12.
 */

public class BitmapTextUtil {
    public static final int KKMultiScene2DTextTextureSizeEqualTextSize=0;//纹理大小和文字一样大
    public static final int KKMultiScene2DTextTextureSizeAutoAdjustFixedResolution=1;//给出一个固定分辨率 如果文字宽高在分辨率内则等于这个分辨率 如果文字大于这个分辨率 则按照这个长宽比缩放
    //    KKMultiScene2DTextTextureSizeMode
    public static final int KKMultiScene2DTextVerticalAlignmentCenter=0;//居中 纵向
    public static final int KKMultiScene2DTextVerticalAlignmentTop=1;//上对其
    public static final int KKMultiScene2DTextVerticalAlignmentBottom=2;
//    KKMultiScene2DTextVerticalDrawAlignmentMode;

    public static final int KKMultiScene2DTextHorizontalDrawAlignmentCenter=0;//居中 横向
    public static final int KKMultiScene2DTextHorizontalDrawAlignmentLeft=1;//左对齐
    public static final int KKMultiScene2DTextHorizontalDrawAlignmentRight=2;//右对齐
//    KKMultiScene2DTextHorizontalDrawAlignmentMode;

    public static final int KKMultiScene2DTextHorizontalWrapLayout=0;//水平布局 自动换行
    public static final int  KKMultiScene2DTextVerticalALineLayout=1;//垂直一行
    //    KKMultiScene2DTextLayoutMode;

    public static String textToBitmapAndBindTexture(String textjsonInfo){
        TextInfoModel model = new TextInfoModel();
        model.valueFromJson(textjsonInfo);

        String json = drawTextToBitmap(model);


        return json;//" texture to bitmap to texture true";
    }
    public static String  drawTextToBitmap(TextInfoModel model){

        TextPaint p = new TextPaint();
        p.setAntiAlias(true);
        p.setColor(Color.RED);
        p.setARGB((int)(model.textColor[3]*255),(int)(model.textColor[0]*255),(int)(model.textColor[1]*255),(int)(model.textColor[2]*255));
        if(model.textDrawingMode == 1){
//            p.setStyle(Paint.Style.STROKE);
            p.setStyle(Paint.Style.STROKE);
            p.setARGB((int)(model.strokeColor[3]*255),(int)(model.strokeColor[0]*255),(int)(model.strokeColor[1]*255),(int)(model.strokeColor[2]*255));
            p.setStrokeWidth(model.strokeWidth);
        }
        p.setTypeface(model.font);
        p.setTextSize(model.fontSize);




        Rect rect = new Rect();
        int widthText = 0;
        int heightText = 0;

//        model.textLayoutMode = KKMultiScene2DTextVerticalALineLayout;


        getTextSize(model.userTextStringResult,rect,p);
        if(model.textLayoutMode == KKMultiScene2DTextHorizontalWrapLayout){//横
            widthText = rect.width();
            heightText = (int) (widthText * model.textFixedResolution[1]/ model.textFixedResolution[0]);
            if(heightText<rect.height()){
                heightText = rect.height();
                if(heightText<model.fontSize*7/6){
                    heightText = model.fontSize*7/6;
                }
                widthText = (int) (heightText * model.textFixedResolution[0]/ model.textFixedResolution[1]);
            }
        }else if(model.textLayoutMode == KKMultiScene2DTextVerticalALineLayout){//竖
            widthText = rect.height();
            heightText =  (int) (widthText * model.textFixedResolution[1]/ model.textFixedResolution[0]);
            int countVertical = 0;
            for(int i=0;i<model.userTextStringResult.length;i++){
                if(model.userTextStringResult[i].length()>countVertical){
                    countVertical = model.userTextStringResult[i].length();
                }
            }
            if(heightText<rect.height()*countVertical){
                heightText = rect.height()*countVertical;
                widthText = (int) (heightText * model.textFixedResolution[0]/ model.textFixedResolution[1]);
            }
        }
        rect.set(0,0,widthText,heightText);
        Bitmap bmp = Bitmap.createBitmap(widthText,heightText, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
//        canvas.drawARGB((int)model.textBgColor[0]*255,(int)model.textBgColor[1]*255,(int)model.textBgColor[2]*255,(int)model.textBgColor[3]*255);
//        canvas.drawARGB(255,0,0,255);
        if(model.textLayoutMode == KKMultiScene2DTextHorizontalWrapLayout){

            drawHorizontalText(model,canvas,p,rect,widthText,heightText);
        }else if(model.textLayoutMode == KKMultiScene2DTextVerticalALineLayout){
            drawVerticalText(model,canvas,p,rect,widthText,heightText);
        }

        glBindTexture(GL_TEXTURE_2D, model.textureId);
//        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
//                GL_NEAREST);
//        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
//                GL_NEAREST);
//        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
        GLUtils.texImage2D(GL_TEXTURE_2D, 0, bmp, 0);
        int width = bmp.getWidth();
        int height = bmp.getHeight();
        bmp.recycle();
        bmp = null;
        try{
            JSONObject object=new JSONObject();
            object.put("width",width);
            object.put("height",height);
            return object.toString();
        }catch (Exception e){
            e.printStackTrace();;
        }

        return "";
    }


    public static void drawHorizontalText(TextInfoModel model,Canvas canvas,TextPaint p,Rect textRect,int bmpWidth,int bmpHeight){
        Rect r = new Rect();
        getTextSize(new String[]{model.userTextString},r,p);
        float startY = textRect.height()/2 - r.height()*model.userTextStringResult.length/2 -  r.height()/6;
        for(int i=0;i<model.userTextStringResult.length;i++){
            float x = 0;
            float y = 0;
            Rect rthis = new Rect();
            getTextSize(new String[]{model.userTextStringResult[i]},rthis,p);
            y = startY + -(r.height() -rthis.height())/2+ r.height()*(i+1);
            if(model.textHorizontalDrawAlignmentMode == KKMultiScene2DTextHorizontalDrawAlignmentCenter){//居中
                x = (bmpWidth - rthis.width())/2;
            }else if(model.textHorizontalDrawAlignmentMode == KKMultiScene2DTextHorizontalDrawAlignmentLeft){
                x = 0;
            }else if(model.textHorizontalDrawAlignmentMode == KKMultiScene2DTextHorizontalDrawAlignmentRight){
                x = bmpWidth - rthis.width();
            }
            canvas.drawText(model.userTextStringResult[i],x,y,p);
        }

    }
    public static void drawVerticalText(TextInfoModel model,Canvas canvas,TextPaint p,Rect textRect,int bmpWidth,int bmpHeight){
        Rect allr = new Rect();
        getTextSize(new String[]{model.userTextString},allr,p);
        float startX = textRect.width()/2 - allr.height()*model.userTextStringResult.length/2;
        for(int i=0;i<model.userTextStringResult.length;i++){
            float x = 0;
            float y = 0;

            if(model.textVerticalDrawAlignmentMode == KKMultiScene2DTextVerticalAlignmentCenter){//居中
                y = (bmpHeight - allr.height()*model.userTextStringResult[i].length())/2 - allr.height()/6;
            }else if(model.textVerticalDrawAlignmentMode == KKMultiScene2DTextVerticalAlignmentTop){
                y = 0;
            }else if(model.textVerticalDrawAlignmentMode == KKMultiScene2DTextVerticalAlignmentBottom){
                y = bmpHeight - allr.height()*model.userTextStringResult[i].length();
            }
            for(int j=0;j<model.userTextStringResult[i].length();j++){
                String str = model.userTextStringResult[i].substring(j,j+1);
                Rect r = new Rect();
                getTextSize(new String[]{str},r,p);
                x = startX+(allr.height() - r.width()*7/6)/2+allr.height()*i;
                canvas.drawText(str,x,y+(1+j)*allr.height(),p);
//            y+=r.height();
            }


        }



    }
    public static void getTextSize( String str[], Rect r,TextPaint paint){
        for(int i=0;i<str.length;i++){
            Rect thisr = new Rect();
            paint.getTextBounds(str[i], 0, str[i].length(), thisr);
            if(thisr.width()>r.width()){
                r.set(0,0,thisr.width(),r.height());
            }
            if(thisr.height()>r.height()){
                r.set(0,0,r.width(),thisr.height());
            }
        }
        int height = r.height();
        if(height<paint.getTextSize()*7/6){
            height=(int)paint.getTextSize()*7/6;
        }
        r.set(0,0,r.width(),height*str.length);
    }


    public static String fileToBitmapToTexture(String filepath,int textureId){
        int width = 0;
        int height = 0;
        try{
            if(filepath == null || filepath.length() == 0){
                return "";
            }
            if(textureId == -1){
                return "";
            }
            Bitmap bmp = BitmapFactory.decodeFile(filepath);
            width = bmp.getWidth();
            height = bmp.getHeight();
            glBindTexture(GL_TEXTURE_2D, textureId);
//        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
//                GL_NEAREST);
//        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
//                GL_NEAREST);
//        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            //glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
            GLUtils.texImage2D(GL_TEXTURE_2D, 0, bmp, 0);
            bmp.recycle();
            bmp = null;
        }catch (Exception e){
            e.printStackTrace();
        }

        try{
            JSONObject object=new JSONObject();
            object.put("width",width);
            object.put("height",height);
            return object.toString();
        }catch (Exception e){
            e.printStackTrace();;
        }
        return "";
    }
}
